//
//  DateAddEventViewController.m
//  lvtu
//
//  Created by Apple on 12-3-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DateAddEventViewController.h"
#import "SelectCityViewController.h"
#import "DataPartView.h"

@implementation DateAddEventViewController
@synthesize isTripEdit;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
        isTripEdit = NO;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [dateDetailView release];
    [super dealloc];
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.navigationItem.title = @"旅途设计";
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    [tempLabel setText:@"旅途设计"];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setTextAlignment:UITextAlignmentCenter];
    [tempLabel setTextColor:[UIColor colorWithRed:111/255.0 green:117/255.0 blue:81/255.0 alpha:1.0]];
    [tempLabel setFont:[UIFont boldSystemFontOfSize:20]];
    self.navigationItem.titleView = tempLabel;
    [tempLabel release];
    tempLabel = nil;
    
    self.tabBarController.view.hidden = YES;
    viewArray1 = [[NSMutableArray alloc] init];
    viewArray2 = [[NSMutableArray alloc] init];
    viewArray3 = [[NSMutableArray alloc] init];
    viewArray4 = [[NSMutableArray alloc] init];
    viewArray5 = [[NSMutableArray alloc] init];
    
    UIImageView* backgroudImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+300)];
    backgroudImageView.image = [UIImage imageNamed:@"bg_texture.png"];
    [self.view addSubview:backgroudImageView];
    [backgroudImageView release];
    
    UIImage* backImage = [UIImage imageNamed:@"btn_back_normal.png"];
    UIButton* backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    backbutton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
    [backbutton setBackgroundImage:backImage forState:UIControlStateNormal];
    [backbutton setBackgroundImage:[UIImage imageNamed:@"btn_back_press.png"] forState:UIControlStateHighlighted];
    [backbutton addTarget:self action:@selector(backButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    [backButtonItem release];
    
    UILabel* localLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 60, 200, 10)];
    localLabel.text = @"选择此时间段所在的城市或地点:";
    localLabel.font = [UIFont systemFontOfSize:10];
    localLabel.backgroundColor = [UIColor clearColor];
    localLabel.textColor = [UIColor colorWithRed:122/255.0 green:143/255.0 blue:68/255.0 alpha:1];
    [self.view addSubview:localLabel];
    [localLabel release];
    
    dataPartView = [[DataPartView alloc] initWithFrame:CGRectMake(0, 70, 320, 50)];
    dataPartView.partLabel.text = @"目的地：";
    dataPartView.eventCountLabel.text = @"";
    [dataPartView setButtonStyle:EDITBUTTON];
    dataPartView.delegate = self;
    [self.view addSubview:dataPartView];
    [dataPartView release];
    
    UILabel* eventLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 130, 200, 10)];
    eventLabel.text = @"选择添加此时间段的具体事件:";
    eventLabel.font = [UIFont systemFontOfSize:10];
    eventLabel.backgroundColor = [UIColor clearColor];
    eventLabel.textColor = [UIColor colorWithRed:122/255.0 green:143/255.0 blue:68/255.0 alpha:1];
    [self.view addSubview:eventLabel];
    [eventLabel release];
    
    eventListScrollVeiw = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 140, 320, 245)];
    eventListScrollVeiw.backgroundColor = [UIColor clearColor];
    //eventScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+280);
    //eventScrollVeiw.clipsToBounds = YES;
    eventListScrollVeiw.scrollsToTop = YES;
    eventListScrollVeiw.showsVerticalScrollIndicator = NO;
    [self.view addSubview:eventListScrollVeiw];
    [eventListScrollVeiw release];
    
    dataPartView1 = [[DataPartView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    dataPartView1.partLabel.text = @"游： ";
    dataPartView1.delegate = self;
    dataPartView1.elementType = TYPE_TRAVEL;
    if (![dataPartView1 setEventShow:viewArray1]) {
        dataPartView1.frame = CGRectMake(0, 0, 320, 50);
    }
    [eventListScrollVeiw addSubview:dataPartView1];
    [dataPartView1 release];
    
    dataPartView2 = [[DataPartView alloc] initWithFrame:CGRectMake(0, 0 + dataPartView1.frame.size.height, 320, 150)];
    dataPartView2.partLabel.text = @"吃： ";
    dataPartView2.delegate = self;
    dataPartView2.elementType = TYPE_EAT ;
    if (![dataPartView2 setEventShow:viewArray2]) {
        dataPartView2.frame = CGRectMake(0, 0 + dataPartView1.frame.size.height, 320, 50);
    }
    [eventListScrollVeiw addSubview:dataPartView2];
    [dataPartView2 release];
    
    dataPartView3 = [[DataPartView alloc] initWithFrame:CGRectMake(0, 0+dataPartView2.frame.origin.y+dataPartView2.frame.size.height, 320, 150)];
    dataPartView3.partLabel.text = @"住： ";
    dataPartView3.delegate = self;
    dataPartView3.elementType = TYPE_LIVE;
    if (![dataPartView3 setEventShow:viewArray3]) {
        dataPartView3.frame = CGRectMake(0, 0+dataPartView2.frame.origin.y+dataPartView2.frame.size.height, 320, 50);
    }
    [eventListScrollVeiw addSubview:dataPartView3];
    [dataPartView3 release];
    
    dataPartView4 = [[DataPartView alloc] initWithFrame:CGRectMake(0, 0+dataPartView3.frame.origin.y+dataPartView3.frame.size.height, 320, 150)];
    dataPartView4.partLabel.text = @"购： ";
    dataPartView4.delegate = self;
    dataPartView4.elementType = TYPE_BUY;
    if (![dataPartView4 setEventShow:viewArray4]) {
        dataPartView4.frame = CGRectMake(0, 0+dataPartView3.frame.origin.y+dataPartView3.frame.size.height, 320, 50);
    }
    [eventListScrollVeiw addSubview:dataPartView4];
    [dataPartView4 release];
    
    dataPartView5 = [[DataPartView alloc] initWithFrame:CGRectMake(0, 0+dataPartView4.frame.origin.y+dataPartView4.frame.size.height, 320, 150)];
    dataPartView5.partLabel.text = @"娱： ";
    dataPartView5.delegate = self;
    dataPartView5.elementType = TYPE_PLAY;
    if (![dataPartView5 setEventShow:viewArray5]) {
        dataPartView5.frame = CGRectMake(0, 0+dataPartView4.frame.origin.y+dataPartView4.frame.size.height, 320, 50);
    }
    [eventListScrollVeiw addSubview:dataPartView5];
    [dataPartView5 release];
    
    eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, 250);
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(updateCitySelect:)
               name:UpdateCitySelect
             object:nil];
    
    [nc addObserver:self selector:@selector(updateEventChoice:)
               name:UpdateEventChoice
             object:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UpdateCitySelect object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UpdateEventChoice object:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}

- (void)setDateContent:(NSString*)title date:(NSString*)date
{
    dateDetailView = [[DateDetailView alloc] initWithFrame:CGRectMake(10, 25, 303, 23)];
    dateDetailView.dateLabel.text = title;
    dateDetailView.dateLabel.font = [UIFont systemFontOfSize:14];
    dateDetailView.dateLabel.backgroundColor = [UIColor clearColor];
    CGRect frame = dateDetailView.detailLabel.frame;
    
    date = [date substringToIndex:[date rangeOfString:@"："].location];
    int tempWidth = [date sizeWithFont:[UIFont systemFontOfSize:17.0f]
                     constrainedToSize:CGSizeMake(9999, 23) lineBreakMode:UILineBreakModeCharacterWrap].width;
    
    dateDetailView.detailLabel.frame = CGRectMake(dateDetailView.frame.size.width - tempWidth - 10, frame.origin.y, tempWidth+10, frame.size.height);
    dateDetailView.detailLabel.text = date;
    dateDetailView.detailLabel.font = [UIFont boldSystemFontOfSize:17];
    dateDetailView.detailLabel.textColor = [UIColor colorWithRed:252/255.0 green:100/255.0 blue:22/255.0 alpha:1];
    dateDetailView.detailLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:dateDetailView];
}

- (void)backButtonPress:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)updateCitySelect:(NSNotification*)note
{
    dataPartView.eventCountLabel.text = [note.userInfo objectForKey:@"name"];
}

- (void)updateEventChoice:(NSNotification*)note
{
    
}

- (void)updateAllEventByEdit:(NSDictionary*)dict 
{
    dataPartView.eventCountLabel.text = [dict objectForKey:@"city"];
    for (CityInfo* info in [[DataSource shareInstance] cityArray]) {
        if ([info.name isEqualToString:dataPartView.eventCountLabel.text]) {
            [[DataSource shareInstance] setCityinfoEdit:info];
            break;
        }
    }
    
    NSMutableArray* datePartDict = [[NSMutableArray alloc] initWithArray:[dict objectForKey:@"list"]];
    NSMutableArray* shopinfoArray = [[NSMutableArray alloc] init ];
    for (NSDictionary* info in datePartDict) {
        if ([info isKindOfClass:[PlaceInfo class]]) {
            [shopinfoArray addObject:info];
        }
        else if ([[info objectForKey:@"type"] isEqual:@"1"]) {
            [shopinfoArray addObject:info];
        }
    }
    [datePartDict removeObjectsInArray:shopinfoArray];
    [viewArray1 addObjectsFromArray:shopinfoArray];
    if ([viewArray1 count] > 0) {
        [dataPartView1 setEventShow:viewArray1];
        dataPartView1.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView1.eventArray count]];
        dataPartView1.frame = CGRectMake(0, 0, 320, 180);
    }
    
    [shopinfoArray removeAllObjects];
    for (NSDictionary* info in datePartDict) {
        if ([info isKindOfClass:[PlaceInfo class]]) {
            [shopinfoArray addObject:info];
        }
        else if ([[info objectForKey:@"type"] isEqual:@"2"]) {
            [shopinfoArray addObject:info];
        }
    }
    [datePartDict removeObjectsInArray:shopinfoArray];
    [viewArray2 addObjectsFromArray:shopinfoArray];
    NSInteger height = dataPartView1.frame.size.height > 50 ? dataPartView1.frame.size.height-30:dataPartView1.frame.size.height;
    if ([viewArray2 count] > 0) {
        [dataPartView2 setEventShow:viewArray2];
        dataPartView2.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView2.eventArray count]];
        dataPartView2.frame = CGRectMake(0, 0 + dataPartView1.frame.origin.y + height, 320, 180);
    }
    else {
        dataPartView2.frame = CGRectMake(0, 0 + dataPartView1.frame.origin.y + height, 320, 50);
    }
    
    [shopinfoArray removeAllObjects];
    for (NSDictionary* info in datePartDict) {
        if ([info isKindOfClass:[PlaceInfo class]]) {
            [shopinfoArray addObject:info];
        }
        else if ([[info objectForKey:@"type"] isEqual:@"3"]) {
            [shopinfoArray addObject:info];
        }
    }
    [datePartDict removeObjectsInArray:shopinfoArray];
    [viewArray3 addObjectsFromArray:shopinfoArray];
    height = dataPartView2.frame.size.height > 50 ? dataPartView2.frame.size.height-30:dataPartView2.frame.size.height;
    if ([viewArray3 count] > 0) {
        [dataPartView3 setEventShow:viewArray3];
        dataPartView3.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView3.eventArray count]];
        dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + height, 320, 180);
    }
    else {
        dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + height, 320, 50);
    }
    
    [shopinfoArray removeAllObjects];
    for (NSDictionary* info in datePartDict) {
        if ([info isKindOfClass:[PlaceInfo class]]) {
            [shopinfoArray addObject:info];
        }
        else if ([[info objectForKey:@"type"] isEqual:@"4"]) {
            [shopinfoArray addObject:info];
        }
    }
    [datePartDict removeObjectsInArray:shopinfoArray];
    [viewArray4 addObjectsFromArray:shopinfoArray];
    height = dataPartView3.frame.size.height > 50 ? dataPartView3.frame.size.height-30:dataPartView3.frame.size.height;
    if ([viewArray4 count] > 0) {
        [dataPartView4 setEventShow:viewArray4];
        dataPartView4.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView4.eventArray count]];
        dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + height, 320, 180);
    }
    else {
        dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + height, 320, 50);
    }
    
    [shopinfoArray removeAllObjects];
    for (NSDictionary* info in datePartDict) {
        if ([info isKindOfClass:[PlaceInfo class]]) {
            [shopinfoArray addObject:info];
        }
        else if ([[info objectForKey:@"type"] isEqual:@"5"]) {
            [shopinfoArray addObject:info];
        }
    }
    [datePartDict removeObjectsInArray:shopinfoArray];
    [viewArray5 addObjectsFromArray:shopinfoArray];
    height = dataPartView4.frame.size.height > 50 ? dataPartView4.frame.size.height-30:dataPartView4.frame.size.height;
    if ([viewArray5 count] > 0) {
        [dataPartView5 setEventShow:viewArray5];
        dataPartView5.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView5.eventArray count]];
        dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + height, 320, 180);
        
        eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+180);
    }
    else {
        dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + height, 320, 50);
        
        eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
    }
}

- (void)updateAllEvent
{
    NSString* cityId = [[DataSource shareInstance] getCityId];
    if (cityId) {
        for (CityInfo* info in [[DataSource shareInstance] cityArray]) {
            if ([info.cityid isEqualToString:cityId]) {
                dataPartView.eventCountLabel.text = info.name;
            }
        }
    }

    [viewArray1 addObjectsFromArray:[[DataSource shareInstance] getEvent:[[DataSource shareInstance] getDatePartForNow] eventPartNum:0]];
    if ([viewArray1 count] > 0) {
        [dataPartView1 setEventShow:viewArray1];
        dataPartView1.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView1.eventArray count]];
        dataPartView1.frame = CGRectMake(0, 0, 320, 180);
    }
    
    [viewArray2 addObjectsFromArray:[[DataSource shareInstance] getEvent:[[DataSource shareInstance] getDatePartForNow] eventPartNum:1]];
    NSInteger height = dataPartView1.frame.size.height > 50 ? dataPartView1.frame.size.height-30:dataPartView1.frame.size.height;
    if ([viewArray2 count] > 0) {
        [dataPartView2 setEventShow:viewArray2];
        dataPartView2.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView2.eventArray count]];
        dataPartView2.frame = CGRectMake(0, 0 + dataPartView1.frame.origin.y + height, 320, 180);
    }
    else {
        dataPartView2.frame = CGRectMake(0, 0 + dataPartView1.frame.origin.y + height, 320, 50);
    }
    
    
    [viewArray3 addObjectsFromArray:[[DataSource shareInstance] getEvent:[[DataSource shareInstance] getDatePartForNow] eventPartNum:2]];
    height = dataPartView2.frame.size.height > 50 ? dataPartView2.frame.size.height-30:dataPartView2.frame.size.height;
    if ([viewArray3 count] > 0) {
        [dataPartView3 setEventShow:viewArray3];
        dataPartView3.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView3.eventArray count]];
        dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + height, 320, 180);
    }
    else {
        dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + height, 320, 50);
    }
    
    [viewArray4 addObjectsFromArray:[[DataSource shareInstance] getEvent:[[DataSource shareInstance] getDatePartForNow] eventPartNum:3]];
    height = dataPartView3.frame.size.height > 50 ? dataPartView3.frame.size.height-30:dataPartView3.frame.size.height;
    if ([viewArray4 count] > 0) {
        [dataPartView4 setEventShow:viewArray4];
        dataPartView4.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView4.eventArray count]];
        dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + height, 320, 180);
    }
    else {
        dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + height, 320, 50);
    }
    
    [viewArray5 addObjectsFromArray:[[DataSource shareInstance] getEvent:[[DataSource shareInstance] getDatePartForNow] eventPartNum:4]];
    height = dataPartView4.frame.size.height > 50 ? dataPartView4.frame.size.height-30:dataPartView4.frame.size.height;
    if ([viewArray5 count] > 0) {
        [dataPartView5 setEventShow:viewArray5];
        dataPartView5.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView5.eventArray count]];
        dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + height, 320, 180);
        
        eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+180);
    }
    else {
        dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + height, 320, 50);
        
        eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
    }
}

- (BOOL)addEvent:(PlaceInfo*)info type:(ElementType)type
{
    if (type == TYPE_TRAVEL) {
        if ([viewArray1 count] == 10) {
            return NO;
        }
        for (PlaceInfo* tempinfo in viewArray1) {
            if ([tempinfo isKindOfClass:[PlaceInfo class]]) {
                if ([tempinfo.shopid isEqualToString:info.shopid]) {
                    return NO;
                }
            }
            else {
                if ([[((NSDictionary*)tempinfo) objectForKey:@"c_shop_id"] isEqualToString:info.shopid]) {
                    return NO;
                }
            }
        }
        if ([viewArray1 count] > 0) 
        {
            [dataPartView1 setButtonStyle:BOTHBUTTON];
        }
        [viewArray1 addObject:info];
        [dataPartView1 setEventShow:viewArray1];
        dataPartView1.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView1.eventArray count]];
        dataPartView1.frame = CGRectMake(0, 0, 320, 180);
        dataPartView2.frame = CGRectMake(0, 0 + dataPartView1.frame.size.height-30, 320, dataPartView2.frame.size.height);
        dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + dataPartView2.frame.size.height, 320, dataPartView3.frame.size.height);
        dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + dataPartView3.frame.size.height, 320, dataPartView4.frame.size.height);
        dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + dataPartView4.frame.size.height, 320, dataPartView5.frame.size.height);     
        
        eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
        
        if (isTripEdit) {
            [[DataSource shareInstance] setEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:0 event:info];
        }
        else {
            [[DataSource shareInstance] setEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:0 event:info];
        }
        return YES;
    }
    else if (type == TYPE_EAT )
    {
        if ([viewArray2 count] == 10) {
            return NO;
        }
        for (PlaceInfo* tempinfo in viewArray2) {
            if ([tempinfo isKindOfClass:[PlaceInfo class]]) {
                if ([tempinfo.shopid isEqualToString:info.shopid]) {
                    return NO;
                }
            }
            else {
                if ([[((NSDictionary*)tempinfo) objectForKey:@"c_shop_id"] isEqualToString:info.shopid]) {
                    return NO;
                }
            }
        }
        if ([viewArray2 count] > 0) 
        {
            [dataPartView2 setButtonStyle:BOTHBUTTON];
        }
        [viewArray2 addObject:info];
        [dataPartView2 setEventShow:viewArray2];
        dataPartView2.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView2.eventArray count]];
        NSInteger height = dataPartView1.frame.size.height > 50 ? dataPartView1.frame.size.height-30:dataPartView1.frame.size.height;
        dataPartView2.frame = CGRectMake(0, 0 + dataPartView1.frame.origin.y + height, 320, 180);
        dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + dataPartView2.frame.size.height-30, 320, dataPartView3.frame.size.height);
        dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + dataPartView3.frame.size.height, 320, dataPartView4.frame.size.height);
        dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + dataPartView4.frame.size.height, 320, dataPartView5.frame.size.height);     
        
        eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
        if (isTripEdit) {
            [[DataSource shareInstance] setEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:1 event:info];
        }
        else { 
            [[DataSource shareInstance] setEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:1 event:info];
        }
        return YES;
    }
    else if (type == TYPE_LIVE)
    {
        if ([viewArray3 count] == 10) {
            return NO;
        }
        for (PlaceInfo* tempinfo in viewArray3) {
            if ([tempinfo isKindOfClass:[PlaceInfo class]]) {
                if ([tempinfo.shopid isEqualToString:info.shopid]) {
                    return NO;
                }
            }
            else {
                if ([[((NSDictionary*)tempinfo) objectForKey:@"c_shop_id"] isEqualToString:info.shopid]) {
                    return NO;
                }
            }
        }
        if ([viewArray3 count] > 0) 
        {
            [dataPartView3 setButtonStyle:BOTHBUTTON];
        }
        [viewArray3 addObject:info];
        [dataPartView3 setEventShow:viewArray3];
        dataPartView3.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView3.eventArray count]];
        NSInteger height = dataPartView2.frame.size.height > 50 ? dataPartView2.frame.size.height-30:dataPartView2.frame.size.height;
        dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + height, 320, 180);
        dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + dataPartView3.frame.size.height-30, 320, dataPartView4.frame.size.height);
        dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + dataPartView4.frame.size.height, 320, dataPartView5.frame.size.height);     
        
        eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);    
        
        if (isTripEdit) {
            [[DataSource shareInstance] setEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:2 event:info];
        }
        else {
            [[DataSource shareInstance] setEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:2 event:info];
        }
        return YES;
    }
    else if (type == TYPE_BUY)
    {
        if ([viewArray4 count] == 10) {
            return NO;
        }
        for (PlaceInfo* tempinfo in viewArray4) {
            if ([tempinfo isKindOfClass:[PlaceInfo class]]) {
                if ([tempinfo.shopid isEqualToString:info.shopid]) {
                    return NO;
                }
            }
            else {
                if ([[((NSDictionary*)tempinfo) objectForKey:@"c_shop_id"] isEqualToString:info.shopid]) {
                    return NO;
                }
            }
        }
        if ([viewArray4 count] > 0) 
        {
            [dataPartView4 setButtonStyle:BOTHBUTTON];
        }
        [viewArray4 addObject:info];
        [dataPartView4 setEventShow:viewArray4];
        dataPartView4.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView4.eventArray count]];
        NSInteger height = dataPartView3.frame.size.height > 50 ? dataPartView3.frame.size.height-30:dataPartView3.frame.size.height;
        dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + height, 320, 180);
        dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + dataPartView4.frame.size.height-30, 320, dataPartView5.frame.size.height);     
        
        eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
        
        if (isTripEdit) {
            [[DataSource shareInstance] setEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:3 event:info];
        }
        else {
            [[DataSource shareInstance] setEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:3 event:info];
        }
        return YES;
    }
    else if (type == TYPE_PLAY)
    {
        if ([viewArray5 count] == 10) {
            return NO;
        }
        for (PlaceInfo* tempinfo in viewArray5) {
            if ([tempinfo isKindOfClass:[PlaceInfo class]]) {
                if ([tempinfo.shopid isEqualToString:info.shopid]) {
                    return NO;
                }
            }
            else {
                if ([[((NSDictionary*)tempinfo) objectForKey:@"c_shop_id"] isEqualToString:info.shopid]) {
                    return NO;
                }
            }
        }
        if ([viewArray5 count] > 0) 
        {
            [dataPartView5 setButtonStyle:BOTHBUTTON];
        }
        [viewArray5 addObject:info];
        [dataPartView5 setEventShow:viewArray5];
        dataPartView5.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView5.eventArray count]];
        NSInteger height = dataPartView4.frame.size.height > 50 ? dataPartView4.frame.size.height-30:dataPartView4.frame.size.height;
        dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + height, 320, 180);     
        
        eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+180);
        
        if (isTripEdit) {
            [[DataSource shareInstance] setEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:4 event:info];
        }
        else {
            [[DataSource shareInstance] setEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:4 event:info];
        }
        return YES;
    }
    return NO;
}

- (BOOL)delEvent:(PlaceInfo*)info type:(ElementType)type
{
    if (type == TYPE_TRAVEL) {
        [viewArray1 removeObject:info];
        if ([viewArray1 count] == 0) 
        {
            [dataPartView1 setButtonStyle:ADDBUTTON];
            dataPartView1.frame = CGRectMake(0, 0, 320, 50);
            dataPartView2.frame = CGRectMake(0, 0 + dataPartView1.frame.size.height, 320, dataPartView2.frame.size.height);
            dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + dataPartView2.frame.size.height, 320, dataPartView3.frame.size.height);
            dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + dataPartView3.frame.size.height, 320, dataPartView4.frame.size.height);
            dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + dataPartView4.frame.size.height, 320, dataPartView5.frame.size.height);     
            
            eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
        }
        [dataPartView1 setEventShow:viewArray1];
        dataPartView1.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView1.eventArray count]];
        
        if (isTripEdit) {
            [[DataSource shareInstance] delEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:0 event:info];
        }
        else {
            [[DataSource shareInstance] delEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:0 event:info];
        }
        return YES;
    }
    else if (type == TYPE_EAT)
    {
        [viewArray2 removeObject:info];
        if ([viewArray2 count] == 0) 
        {
            [dataPartView2 setButtonStyle:ADDBUTTON];
            NSInteger height = dataPartView1.frame.size.height > 50 ? dataPartView1.frame.size.height-30:dataPartView1.frame.size.height;
            dataPartView2.frame = CGRectMake(0, 0 + height, 320, 50);
            dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + dataPartView2.frame.size.height, 320, dataPartView3.frame.size.height);
            dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + dataPartView3.frame.size.height, 320, dataPartView4.frame.size.height);
            dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + dataPartView4.frame.size.height, 320, dataPartView5.frame.size.height);     
            
            eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
        }
        [dataPartView2 setEventShow:viewArray2];
        dataPartView2.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView2.eventArray count]];
        
        if (isTripEdit) {
            [[DataSource shareInstance] delEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:1 event:info];
        }
        else {
            [[DataSource shareInstance] delEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:1 event:info];
        }
        return YES;
    }
    else if (type == TYPE_LIVE)
    {
        [viewArray3 removeObject:info];
        if ([viewArray3 count] == 0) 
        {
            [dataPartView3 setButtonStyle:ADDBUTTON];
            NSInteger height = dataPartView2.frame.size.height > 50 ? dataPartView2.frame.size.height-30:dataPartView2.frame.size.height;
            dataPartView3.frame = CGRectMake(0, 0 + dataPartView2.frame.origin.y + height, 320, 50);
            dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + dataPartView3.frame.size.height, 320, dataPartView4.frame.size.height);
            dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + dataPartView4.frame.size.height, 320, dataPartView5.frame.size.height);     
            
            eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
        }
        [dataPartView3 setEventShow:viewArray3];
        dataPartView3.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView3.eventArray count]];
        
        if (isTripEdit) {
            [[DataSource shareInstance] delEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:2 event:info];
        }
        else {
            [[DataSource shareInstance] delEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:2 event:info];
        }
        return YES;
    }
    else if (type == TYPE_BUY)
    {
        [viewArray4 removeObject:info];
        if ([viewArray4 count] == 0) 
        {
            [dataPartView4 setButtonStyle:ADDBUTTON];
            NSInteger height = dataPartView3.frame.size.height > 50 ? dataPartView3.frame.size.height-30:dataPartView3.frame.size.height;
            dataPartView4.frame = CGRectMake(0, 0 + dataPartView3.frame.origin.y + height, 320, 50);
            dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + dataPartView4.frame.size.height, 320, dataPartView5.frame.size.height);     
            
            eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
        }
        [dataPartView4 setEventShow:viewArray4];
        dataPartView4.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView4.eventArray count]];
        
        if (isTripEdit) {
            [[DataSource shareInstance] delEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:3 event:info];
        }
        else {
            [[DataSource shareInstance] delEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:3 event:info];
        }
        return YES;
    }
    else if (type == TYPE_PLAY)
    {
        [viewArray5 removeObject:info];
        if ([viewArray5 count] > 0) 
        {
            [dataPartView5 setButtonStyle:ADDBUTTON];
            NSInteger height = dataPartView4.frame.size.height > 50 ? dataPartView4.frame.size.height-30:dataPartView4.frame.size.height;
            dataPartView5.frame = CGRectMake(0, 0 + dataPartView4.frame.origin.y + height, 320, 50);     
            
            eventListScrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width, dataPartView4.frame.origin.y + dataPartView4.frame.size.height+50);
        }
        [dataPartView5 setEventShow:viewArray5];
        dataPartView5.eventCountLabel.text = [NSString stringWithFormat:@"%d事件", [dataPartView5.eventArray count]];
        
        if (isTripEdit) {
            [[DataSource shareInstance] delEventCountEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:4 event:info];
        }
        else {
            [[DataSource shareInstance] delEventCount:[[DataSource shareInstance] getDatePartForNow] eventPartNum:4 event:info];
        }
        return YES;
    }
    return NO;
}

- (void)touchUpInsideEditViewCell:(NSString*)IndexText  button:(NSInteger)buttontype elementType:(ElementType)type
{
    NSRange range = [IndexText rangeOfString:@"目的地"];
    if (range.location != NSNotFound ) {
        SelectCityViewController* selectCityViewController = [[SelectCityViewController alloc] init];
        if (isTripEdit) {
            selectCityViewController.isEdit = YES;
        }
        else {
            selectCityViewController.isEdit = NO;
        }
        [self.navigationController pushViewController:selectCityViewController animated:YES];
        [selectCityViewController release];
    }
    else
    {
        if (isTripEdit) {
            if (![[DataSource shareInstance]getCityIdEdit]) {
                UIAlertView* baseAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"还未选择城市哟~" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [baseAlert show];
                [baseAlert release];
                return;
            }
        }
        else {
            if (![[DataSource shareInstance]getCityId]) {
                UIAlertView* baseAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"还未选择城市哟~" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [baseAlert show];
                [baseAlert release];
                return;
            }
        }
        if (buttontype == 0) {
            EventViewController* eventViewController = [[EventViewController alloc] initWithType:NO edit:isTripEdit];
            eventViewController.elementType = type;
            eventViewController.delegate = self;
            [self.navigationController pushViewController:eventViewController animated:YES];
            [eventViewController release];
        }
        else
        {
            EventViewController* eventViewController = [[EventViewController alloc] initWithType:YES edit:isTripEdit];
            eventViewController.delegate = self;
            eventViewController.elementType = type;
            if (isTripEdit) {
                [eventViewController.m_ShowAry addObjectsFromArray:[[DataSource shareInstance] getEventEdit:[[DataSource shareInstance] getDatePartForNowEdit] eventPartNum:buttontype-1]];
            }
            else {
                [eventViewController.m_ShowAry addObjectsFromArray:[[DataSource shareInstance] getEvent:[[DataSource shareInstance] getDatePartForNow] eventPartNum:buttontype-1]];
            }
            [self.navigationController pushViewController:eventViewController animated:YES];
            [eventViewController release];
        }
    }
}

@end
